﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NLUNZ
{
    public partial class OnlineBooking : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                this.onlineBookingForm.Visible = true;
                this.onlineBookingConfirmation.Visible = false;
                this.submitConfirmation.Visible = false;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ConfirmationDataBind();
            this.onlineBookingForm.Visible = false;
            this.onlineBookingConfirmation.Visible = true;
            this.submitConfirmation.Visible = false;
        }

        private void ConfirmationDataBind()
        {
            this.lblContact.Text = this.txtContact.Text;
            this.lblAddress.Text = this.txtAddress.Text;
            this.lblPhone.Text = this.txtPhone.Text;
            var selectedPickupDate = this.RadDateTimePicker1.SelectedDate == null ? DateTime.Now : this.RadDateTimePicker1.SelectedDate;
            this.lblPickupDateTime.Text = ((DateTime)selectedPickupDate).ToString("yyyy-MM-dd HH:mm:ss");
            this.lblWeight.Text = this.txtWeight.Text;
            this.lblEmail.Text = this.txtEmail.Text;
            this.lblProduct.Text = this.txtProduct.Text;
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            SaveBooking();
            this.onlineBookingForm.Visible = false;
            this.onlineBookingConfirmation.Visible = false;
            this.submitConfirmation.Visible = true;

        }

        private void SaveBooking()
        {
            decimal weight = Convert.ToDecimal(txtWeight.Text);
            Booking booking = new Booking()
                                  {
                                      Contact = txtContact.Text,
                                      Address = txtAddress.Text,
                                      PickupDateTime = RadDateTimePicker1.SelectedDate,
                                      Email = txtEmail.Text,
                                      Phone = txtPhone.Text,
                                      ProductName = txtProduct.Text,
                                      Weight = weight
                                  };

            EmailBookingInfo(booking);
            Repository.AddBooking(booking);
        }

        private void EmailBookingInfo(Booking booking)
        {
            string subject = "New Online Booking from - " + booking.Contact;
            StringBuilder body = new StringBuilder();
            body.AppendFormat("<div><table>");
            body.AppendFormat("<tr><td>联系人</td><td>{0}</td></tr>",booking.Contact);
            body.AppendFormat("<tr><td>地址</td><td>{0}</td></tr>",booking.Address);
            body.AppendFormat("<tr><td>联系电话</td><td>{0}</td></tr>",booking.Phone);
            body.AppendFormat("<tr><td>预约取件时间</td><td>{0}</td></tr>",booking.PickupDateTime);
            body.AppendFormat("<tr><td>货物名称</td><td>{0}</td></tr>",booking.ProductName);
            body.AppendFormat("<tr><td>估计重量</td><td>{0}</td></tr>",booking.Weight);
            body.AppendFormat("<tr><td>电子邮件</td><td>{0}</td></tr>",booking.Email);
            body.AppendFormat("</table></div>");
            Repository.SendEmail(subject,body.ToString());
        }

    }
}